package Classes.GameDatas // ActionScript file
{
	import Classes.GameUtils.GU_Sound;
	import Classes.GameViews.ViewIcons.ViewIcon;
	import Classes.GameViews.ViewPanels.VP_OpenWinView;
	import Classes.GameViews.ViewPanels.VP_errorPanel;
	import Classes.Interfaces.I_TimeUnit;
	import Classes.Managers.M_Communication;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_Languages;
	import Classes.Managers.M_Timer;
	import Classes.staticinfo.Language;
	import Classes.staticinfo.PlatformInfo;
	
	import UIComponents.IGD_ShopItem;
	import UIComponents.IStore;
	
	import flash.display.DisplayObject;
	import flash.system.ApplicationDomain;

	/**
	 *商店道具类
	 * @author 葛增文
	 *
	 */
	public class GD_ShopItem extends GameData implements I_TimeUnit,IGD_ShopItem
	{
		private var _shopItemType:Number;
		private var _shopConfigObj:Object;
//		private var _itemObj:Object;
		private var _icon:ViewIcon;
		private var _shopItemState:Number;
		private var _listerFlag:Boolean;
		
		private var _istore:IStore;

		public function GD_ShopItem(shopItemType:Number, shopConfigObj:Object,istore:IStore):void
		{
			this._shopItemType=shopItemType;
			this._shopConfigObj=shopConfigObj;
			this._istore=istore
			M_Timer.instance.joinUnit(this);
		}

		public static function creat(creatObj:Object,istore:IStore):GD_ShopItem
		{
			var shopItem:GD_ShopItem=new GD_ShopItem(creatObj.zt_id, creatObj,istore);
			return shopItem;
		}

		public function timeUpdate():void
		{
			/**
			 * 1.首先判断商品有无过时，即下架，如果下架，则通知 GD_Shop 删除此物品，刷新页面，弹出友情提示,把监控事件标志设置成false
			 * 2.判读商品是否为新品，如果是，则改变shopItemState 的值为3
			 * 1小时=60*60=3600秒
			 * 1天=3600*24=86400秒
			 * 1个月=86400*30=2592000秒
			 * 1年=86400*365秒
			 * */
			if(M_Timer.instance.nowTime>this.endTime)
			{
				this.isUpdateTime=false;
				this.shopItemState=0;
				GD_Shop.instance.deleteShopItem(this.shopItemType);
			}else{
				if(M_Timer.instance.nowTime-this.startTime<=2592000)
				{
					this.shopItemState=3;
				}else
				{
					this.shopItemState=this.shopStatue;
				}
			}
		}
		
		/**
		 * 购买 
		 * 
		 */		
		public function sendBuyCommand(pil:int=1):void
		{
			if(M_ConfigData.instance.isOpenQQVip && this.moneyType==0)
			{
				PlatformInfo.instance.qqPlatBuyItemFun(this.shopItemType,this.shopItemImg,pil,(this.itemName+"*"+this.itemName),this.shopItemId);
				
			}else{
				
				if(isReachMoney)
				{
					VP_OpenWinView.instance.openWindon(new (ApplicationDomain.currentDomain.getDefinition("Sys.CommLoadCircleClip") as Class));
					M_Communication.instance.send(12100,{itemType:this.shopItemType,pil:pil});
				}else{
					VP_errorPanel.instance.setErrorMessage(Language.notEnoughMoney);
					VP_errorPanel.instance.enterFrame();
					GU_Sound.instance.playCommSound("System_Prompt");
				}
			}
			
		}
		
		/**
		 * 是否有足够的钱购买物品   (0 表示欧币 1 表示金币 2跟（itemid）表示中间物品)
		 * @return 
		 * 
		 */		
		public function get isReachMoney():Boolean
		{
			var tmpmoney:Number;
			switch(this.moneyType)
			{
				case 0:
					tmpmoney=GD_Player.instance.gold;
					break;
				case 1:
					tmpmoney=GD_Player.instance.money;
					break;
				case 2:
					tmpmoney=GD_Player.instance.exchangeBuildItemNum;
					break;
				default://其他物品时
					tmpmoney=GD_Player.instance.exchangeBuildItemNum;
					break;
			}
			
			return this.price<=tmpmoney;
		}
		
		/**
		 * 是否有足够物品   (用 物品 来购买的)
		 * @return 
		 * 
		 */		
//		public function get isReachItem():Boolean
//		{
//			var tmpmoney:Number=GD_Player.instance.exchangeBuildItemNum;
//			return this.price<=tmpmoney;
//		}
		
		public function get isUpdateTime():Boolean
		{
			return _listerFlag;
		}
		public function set isUpdateTime(value:Boolean):void
		{
			_listerFlag=value;
		}

		/*getter & setter*/
		/**
		 * 商品流水号 
		 * @return 
		 */
		public function get shopItemType():Number
		{
			return _shopItemType;
		}
		
		/**
		 * 商店物品ID 
		 * @return 
		 */		
		public function get shopItemId():Number
		{
			return this.shopConfigObj.zt_item_id;
		}

		/**
		 * 商店的数据的详细信息
		 * @return 
		 */		
		public function get shopConfigObj():Object
		{
			return _shopConfigObj;
		}


		/**
		 *  物品总价格 
		 * @return 
		 */		
		public function get price():Number
		{
			return this.shopConfigObj.zt_money;
		}
		
	
		
		/**
		 * 货币类型 0 表示欧币 1 表示金币 2表示中间物品
		 * @return 
		 */		
		public function get moneyType():Number
		{
			return this.shopConfigObj.zt_money_type;
		}
		
		/**
		 * 每批次数量
		 * @return 
		 */		
		public function get perNum():Number
		{
			return this.shopConfigObj.zt_item_per_num;
		}
		
		/**
		 * 权限要求
		 * @return 
		 */		
		public function get needPower():Number
		{
			return this.shopConfigObj.zt_need_power;
		}
		
		/**
		 * 需求等级
		 * @return 
		 */		
		public function get needLever():Number
		{
			return this.shopConfigObj.zt_need_lever;
		}
		
		/**
		 * 用户是否达到购买此物品的等级 
		 * @return 
		 * 
		 */		
		public function get isReachLv():Boolean
		{			
			return this.needLever<=GD_Player.instance.playerLv;
		}


		
		/**
		 * 是否上架 0下架 1上架 2热卖 3新品  静态值
		 * @return 
		 */		
		private  function get shopStatue():Number
		{
			return this.shopConfigObj.zt_status;
		}
		
		/**
		 * 开始时间
		 * @return 
		 */		
		public function get startTime():Number
		{
			var st:Number=this.changeTimeToMis(String(this.shopConfigObj.zt_start_time));
			return st;
		}
		
		
		/**
		 * 商品下架时间  以秒为单位
		 * @return 
		 */		
		public function get endTime():Number
		{
			var et:Number=this.changeTimeToMis(String(this.shopConfigObj.zt_end_time));
			return et;
		}
		
		/**
		 * 物品类型 
		 * @return 
		 * 
		 */		
		public function get secondType():Number
		{
			return this.shopConfigObj.zt_item_type;
		}
		
		
		/**
		 * 商店物品图片，单独的 PNG图片 
		 * @return 
		 */	
		public function get shopItemImg():String
		{
			return this._istore.getStoreItmePic();
		}

		/**
		 * 物品图片
		 * @return 
		 */	
		public function get shopItemPic():DisplayObject
		{
			return this._istore.getStoreItemIcon();
		}

		/**
		 * 物品名字
		 * @return 
		 */	
		public function get itemName():String
		{
			return this._istore.getStoreItemName();
		}

		/**
		 * 物品描述
		 * @return 
		 */	
		public function get itemDes():String
		{
			return this._istore.getStroeItemDes();
		}
		
		
		public function changeTimeToMis(time:String):Number
		{
			var t:Array=time.split("-");
			var data:Date=new Date(t[0],(t[1]-1),t[2],t[3],t[4],t[5]);
			return (data.getTime()/1000);
		}

		/**
		 *  是否上架 0下架 1上架 2热卖 3新品  此状态会动态改变
		 * @return 
		 * 
		 */		
		public function get shopItemState():Number
		{
			return _shopItemState;
		}

		/**
		 * 设置商品的状态
		 * @param value
		 */		
		public function set shopItemState(value:Number):void
		{
			_shopItemState = value;
		}

		/**
		 * 各种GD对象 
		 * @return 
		 */		
		public function get istore():IStore
		{
			return _istore;
		}
		
		


	}
}